package dnbW.dbW.sucxentdbW;

import dnbW.dbW.DataSource;
import dnbW.entityW.Path;

/**
 * Encapsulates an XML leaf path (a path that may have value and has an ID).
 * It decorates Path class
 * @author truongbaquan
 */
public class LeafPath implements ILeafPath {
    // <editor-fold defaultstate="collapsed" desc="Decorator Pattern">
    private Path path;

    @Override
    public DataSource getDataSource() {
        return this.path.getDataSource();
    }

    @Override
    public String[] getElements() {
        return this.path.getElements();
    }

    @Override
    public String getElementAtLevel(int level) {
        return this.path.getElementAtLevel(level);
    }

    @Override
    public boolean isAttribute() {
        return this.path.isAttribute();
    }

    @Override
    public int getLevel() {
        return this.path.getLevel();
    }

    @Override
    public String toSQLPathExp() {
        return this.path.toSQLPathExp();
    }

    @Override
    public String toPathExp() {
        return this.path.toPathExp();
    }
    // </editor-fold>

    private int pathId;

    @Override
    public int getPathId() {
        return pathId;
    }

    private LeafPath(Path path, int pathId) {
        this.path = path;
        this.pathId = pathId;
    }

    /**
     * Creates a path from its path expression (e.g. uniprot/entry/gene)
     * @param dataSource        Data Source
     * @param pathExpression    Path Expression
     * @param pathId            Leaf path's PathId
     * @return                  New path with the given path expression
     */
    public static LeafPath buildPathFromPathExp(DataSource dataSource, String pathExpression, int pathId) {
        Path path = Path.buildPathFromPathExp(dataSource, pathExpression);
        return new LeafPath(path, pathId);
    }

    /**
     * Creates a path from its SQL path expression (e.g. .uniprot#.entry#.gene#)
     * @param dataSource
     * @param sqlPathExpression
     * @param pathId
     * @return
     */
    public static LeafPath buildPathFromSqlPathExp(DataSource dataSource, String sqlPathExpression, int pathId) {
        Path path = Path.buildPathFromSqlPathExp(dataSource, sqlPathExpression);
        return new LeafPath(path, pathId);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final LeafPath other = (LeafPath) obj;
        if (this.pathId != other.pathId) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 89 * hash + this.pathId;
        return hash;
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof LeafPath) {
            return this.pathId - ((LeafPath)o).pathId;
        }
        else {
            return -1;
        }
    }

    @Override
    public String toString() {
        return path.toString();
    }
}
